﻿#include "ProcessTimer.h"
#include "XyNet/Common/Logger.h"

namespace XyNet
{
	ProcessTimer::ProcessTimer(const string& caption)
		: _caption(caption), _startTime(chrono::steady_clock::now()) {}

	void ProcessTimer::setCaption(const string& caption)
	{
		_caption = caption;
	}

	void ProcessTimer::restart()
	{
		_startTime = chrono::steady_clock::now();
	}

	double ProcessTimer::elapsed() const
	{
		return chrono::duration_cast<chrono::duration<double>>(
			chrono::steady_clock::now() - _startTime).count();
	}

	void ProcessTimer::print()
	{
		boost::format format(_caption + ": %.3ds.");
		format % elapsed();
		Logger::getDefault()->log(format.str());
	}
}